package com.example.demofdfs.web;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.demofdfs.util.FdfsUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/fdfs")
@Api()
public class FdfsController {
	
	@Autowired
	private FdfsUtil fdfsUtil;
	
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ApiOperation(value = "上传文件")
	public Object upload(@ApiParam(value = "文件", name = "file") MultipartFile file) {
		return fdfsUtil.upload(file);
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	@ApiOperation(value = " 删除文件")
	@ApiImplicitParam(name = "filepath", value = "文件路径")
	public Object delete(String filepath)
	{
		fdfsUtil.delete(filepath);
		return "delete file successfull";
	}
	
	@RequestMapping(value = "/download", method = RequestMethod.GET)
	@ApiOperation(value = "文件下载")
	public void download(@ApiParam(value = "文件路径") String filepath, HttpServletResponse response) {
		byte[] b = fdfsUtil.download(filepath);
		try {
			response.getOutputStream().write(b);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
